// pages/user/index.js
// 1. 获取数据库引用
const db = wx.cloud.database().collection("motto");
Page({
  data: {
    userinfo: {},
    textVal:'',
    id:''
    
  },
  //退出登录
  exit(){
    wx.showModal({
      title: '提示',
      content: '您确定要退出登录？',
      success:res=>{
        if(res.confirm){
          wx.clearStorageSync();
          this.setData({
            userinfo: {},
            id: ''
          })
        }
      }
    })
    
  },
  onShow() {
    const userinfo = wx.getStorageSync("userinfo");
    const id = wx.getStorageSync("id");
    
    db.where({ _openid: id }).get({
      success: (res) => {
        console.log(res);
        if(res.data[0]){
        this.setData({
          textVal: res.data[0].text
        })
        }
        else{
          
          //初始化数据
          db.add({
            data: {
              text: "请输入您的座右铭..."
            },
            success: res => {
              console.log(res);
              this.setData({
                textVal: "请输入您的座右铭"
              })
            },
            fail: err => console.log(err)
          })
        }
      },
      fail: err => {
       // console.log(err);
        wx.showToast({
          title: '请求座右铭数据失败',
          icon: 'none',
         
        });
      
      }
    })

    this.setData({ userinfo,id });

  }
})